package com.tfblog.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tfblog.blog.entity.Type;
import com.tfblog.blog.mapper.TypeMapper;
import com.tfblog.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tfeng
 * @since 2021-04-26
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {

    @Autowired
    private TypeMapper typeMapper;

    @Override
    public List<Type> findTypes() {
        //查询所有分类信息
        return typeMapper.selectList(null);
    }

    @Transactional
    @Override
    public Type findTypeByName(String name) {
        QueryWrapper<Type> wrapper = new QueryWrapper<Type>();
        wrapper.eq("name", name);
        Type type = typeMapper.selectOne(wrapper);
        return type;
    }

    @Override
    public Type findTypeById(Long id) {
        return typeMapper.selectById(id);
    }

    @Transactional
    @Override
    public void deleteTypeById(Long id) {
        //根据分类id删除指定的分类信息
        typeMapper.deleteById(id);

    }

    /*
    * 查找所有的分类名称
    * */
    @Override
    public List<String> findTypeNames() {
        List<String> list = typeMapper.findTypeNames();
        return list;
    }
}
